import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";

const routes = [
  {
    path: "/",
    name: "MainLayout",
    meta: {
      keepAlive: true,
      title: "出版社",
    },
    component: () => import("@/layout/MainLayout/MainLayout.vue"),
    children: [
      {
        path: "/",
        name: "Home",
        meta: {
          keepAlive: true,
          title: "首页",
        },
        component: () => import("@/views/Home/Index.vue"),
      },
      {
        path: "/First",
        name: "First",
        meta: {
          keepAlive: false,
          title: "第一题",
        },
        component: () => import("@/views/Subject/First/Index.vue"),
      },
      {
        path: "/Second",
        name: "Second",
        meta: {
          keepAlive: true,
          title: "第二题",
        },
        component: () => import("@/views/Subject/Second/Index.vue"),
      },
      {
        path: "/Third",
        name: "Third",
        meta: {
          keepAlive: true,
          title: "第三题",
        },
        component: () => import("@/views/Subject/Third/Index.vue"),
      },
    ],
  },
  {
    path: "/:catchAll(.*)",
    component: import("@/views/NotFound/Index.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
// 存储初始加载状态
let initialLoad = true;
router.beforeEach((to, fromref, next) => {
  if (initialLoad) {
    // 首次加载（可能是刷新或直接访问URL）
    next({ path: "/", replace: true });
    initialLoad = false;
  } else {
    next();
  }
});
export default router;
